﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ls.Utils
{
    /// <summary>
    /// 上传帮助类
    /// </summary>
    public static class UploadHelper
    {
        /// <summary>
        /// 上传文件到临时文件目录
        /// </summary>
        /// <param name="env"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string SaveToTempFile(IHostingEnvironment env, IFormFile file) {
            return SaveToFile(env,"temp", file);
        }

        /// <summary>
        /// 上传文件到文件目录
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dic">目录</param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string SaveToFile(IHostingEnvironment env,string dic,IFormFile file)
        {
            string tempDic = Path.Combine(env.WebRootPath, dic);
            if (!Directory.Exists(tempDic))
            {
                Directory.CreateDirectory(tempDic);
            }
            string tempFileName = Path.Combine(tempDic, file.FileName);
            using (var stream = new FileStream(tempFileName, FileMode.CreateNew))
            {
                file.CopyTo(stream);
            }
            return tempFileName;
        }
    }
}
